import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../controllers/webview_controller.dart';

class WebviewView extends GetView<WebviewController> {
  const WebviewView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          onPressed: controller.close,
          icon: Icon(
            Icons.arrow_back_ios,
            size: 40.w,
            color: const Color(0xFF333333),
          ),
        ),
        title: Text(
          controller.title,
          style: TextStyle(
            fontSize: 36.sp,
            fontWeight: FontWeight.w500,
            color: const Color(0xFF333333),
          ),
        ),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Obx(() {
          if (controller.isLoading.value) {
            return const Center(
              child: CircularProgressIndicator(color: Color(0xFF047FF7)),
            );
          }

          if (controller.hasError.value) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.error_outline,
                    size: 80.w,
                    color: const Color(0xFF999999),
                  ),
                  SizedBox(height: 20.h),
                  Text(
                    '网络环境不佳，请稍后尝试',
                    style: TextStyle(
                      fontSize: 28.sp,
                      color: const Color(0xFF666666),
                    ),
                  ),
                  SizedBox(height: 40.h),
                  ElevatedButton(
                    onPressed: controller.retry,
                    style: ElevatedButton.styleFrom(
                      backgroundColor: const Color(0xFF047FF7),
                      padding: EdgeInsets.symmetric(
                        horizontal: 40.w,
                        vertical: 20.h,
                      ),
                    ),
                    child: Text(
                      '重试',
                      style: TextStyle(fontSize: 28.sp, color: Colors.white),
                    ),
                  ),
                ],
              ),
            );
          }

          return WebViewWidget(controller: controller.webViewController);
        }),
      ),
    );
  }
}
